/*
 * ShopModel.java
 * business
 *
 * Created by ChenTao on 2016/9/9.
 *
 * Copyright (c) 2016年 yidiandao. All rights reserved.
 */

package com.yidiandao.business.data.model;

import com.yidiandao.business.constant.Common;
import com.yidiandao.business.constant.Urls;
import com.yidiandao.business.data.bean.Goods;
import com.yidiandao.business.data.bean.Shop;
import com.yidiandao.business.data.recyclertype.DoubleGoods;
import com.yidiandao.business.data.request.CollectShopRequest;
import com.yidiandao.business.data.request.QueryHomeShopRequest;
import com.yidiandao.business.data.request.QueryShopCategoryNewGoodsRequest;
import com.yidiandao.business.data.request.QueryShopCategoryPriceNewGoodsRequest;
import com.yidiandao.business.data.request.QueryShopCategoryRequest;
import com.yidiandao.business.data.request.QueryShopCategorySalesGoodsRequest;
import com.yidiandao.business.data.request.QueryShopDetailHeaderRequest;
import com.yidiandao.business.data.request.QueryShopInfoRequest;
import com.yidiandao.business.data.request.QueryShopNewestGoodsRequest;
import com.yidiandao.business.data.request.QueryShopPriceGoodsRequest;
import com.yidiandao.business.data.request.QueryShopSalesRequest;
import com.yidiandao.business.data.response.QueryGoodsListResponse;
import com.yidiandao.business.data.response.QueryHomeShopResponse;
import com.yidiandao.business.data.response.QueryShopCategoryResponse;
import com.yidiandao.business.data.response.QueryShopDetailHeaderResponse;
import com.yidiandao.business.data.response.QueryShopGoodsResponse;
import com.yidiandao.business.data.response.QueryShopInfoResponse;
import com.yidiandao.business.data.net.BaseResponse;
import com.yidiandao.business.data.net.NetBiz;
import com.yidiandao.business.data.net.NetListener;
import com.yidiandao.business.data.net.exception.BizErrorInfo;
import com.yidiandao.business.data.net.exception.NetErrorInfo;
import com.yidiandao.business.ui.widget.listview.DisplayListItem;
import com.yidiandao.business.ui.widget.listview.Functional;
import com.yidiandao.business.ui.widget.listview.displaylistitem.ShopListItem;
import com.yidiandao.business.utils.PreferenceUtils;
import com.yidiandao.business.utils.UIUtils;

import java.util.ArrayList;
import java.util.List;

/** 店铺Model */
public class ShopModel implements IShopModel {

    private NetBiz mNetBiz = new NetBiz();

    private List<DisplayListItem> mDisplayListItems = new ArrayList<>();

    private List<DoubleGoods> mShopAllGoodsGridList = new ArrayList<>();

    private List<DoubleGoods> mCategoryGoodsGridList = new ArrayList<>();

    private List<Goods> mShopAllGoodsList = new ArrayList<>();

    private int shopPager,
            shopNewestGoodsPager,
            shopPriceGoodsPager,
            shopSalesGoodsPager,
            categoryNewGoodsPager,
            categoryPriceGoodsPager,
            categorySalesGoodsPager;

    @Override
    public void loadHomeShopList(
            final boolean isRefreshing, String tag, final OnLoadShopsListener listener) {

        if (isRefreshing) {
            // 下拉刷新需要重新加载数据
            shopPager = 1;
        }

        QueryHomeShopRequest request = new QueryHomeShopRequest();
        request.setUrl(Urls.HOME_HOT_SHOP);
        request.setToken(PreferenceUtils.getToken(UIUtils.getContext()));
        request.setTag(tag);
        request.setPage(shopPager);
        request.setPageSize(Common.PAGER_SIZE);
        mNetBiz.sendGetRequest(request, QueryHomeShopResponse.class,
                new NetListener<QueryHomeShopResponse>() {
                    @Override
                    public void onSuccess(QueryHomeShopResponse response) {
                        // pager 增加
                        shopPager++;

                        if (isRefreshing) {
                            mDisplayListItems.clear();
                        }

                        QueryHomeShopResponse.Data data = response.getData();

                        List<Shop> shopList = data.getShopList();

                        List<DisplayListItem> displayListItems = Functional.serverToClient(shopList,
                                new Functional.Processor<Shop, DisplayListItem>() {
                                    @Override
                                    public DisplayListItem processToClient(Shop shop) {
                                        return new ShopListItem(shop);
                                    }
                                });

                        mDisplayListItems.addAll(displayListItems);

                        if (listener != null) {
                            listener.onSuccess(
                                    mDisplayListItems,
                                    displayListItems.size() == 0,
                                    response.hasMoreData());
                        }
                    }

                    @Override
                    public boolean onError(NetErrorInfo netErrorInfo) {
                        if (listener != null) {
                            listener.onFailed();
                        }
                        return true;
                    }

                    @Override
                    public boolean onFailed(BizErrorInfo bizErrorInfo) {
                        if (listener != null) {
                            listener.onFailed();
                        }
                        return true;
                    }

                    @Override
                    public void onFinal() {
                    }
                });
    }

    @Override
    public void loadShopDetailHeader(
            String supplierId, String tag, final OnLoadShopHeaderListener listener) {

        QueryShopDetailHeaderRequest request = new QueryShopDetailHeaderRequest();

        request.setUrl(Urls.SHOP_DETAIL_HEADER);
        request.setSupplierId(supplierId);
        request.setTag(tag);
        request.setToken(PreferenceUtils.getToken(UIUtils.getContext()));

        mNetBiz.sendGetRequest(
                request,
                QueryShopDetailHeaderResponse.class,
                new NetListener<QueryShopDetailHeaderResponse>() {
                    @Override
                    public void onSuccess(QueryShopDetailHeaderResponse response) {
                        if (listener != null) {
                            listener.onSuccess(response.getData());
                        }
                    }

                    @Override
                    public boolean onError(NetErrorInfo netErrorInfo) {
                        if (listener != null) {
                            listener.onFailed();
                        }
                        return true;
                    }

                    @Override
                    public boolean onFailed(BizErrorInfo bizErrorInfo) {
                        if (listener != null) {
                            listener.onFailed();
                        }
                        return true;
                    }

                    @Override
                    public void onFinal() {
                    }
                });
    }

    @Override
    public void loadShopInfo(String supplierId, String tag, final OnLoadShopInfoListener listener) {

        QueryShopInfoRequest request = new QueryShopInfoRequest();
        request.setUrl(Urls.SHOP_INFO);
        request.setTag(tag);
        request.setSupplierId(supplierId);
        request.setToken(PreferenceUtils.getToken(UIUtils.getContext()));

        mNetBiz.sendGetRequest(
                request,
                QueryShopInfoResponse.class,
                new NetListener<QueryShopInfoResponse>() {
                    @Override
                    public void onSuccess(QueryShopInfoResponse response) {
                        if (listener != null) {
                            listener.onSuccess(response.getData());
                        }
                    }

                    @Override
                    public boolean onError(NetErrorInfo netErrorInfo) {
                        return true;
                    }

                    @Override
                    public boolean onFailed(BizErrorInfo bizErrorInfo) {
                        return true;
                    }

                    @Override
                    public void onFinal() {
                    }
                });
    }

    @Override
    public void loadShopNewestGoods(
            final boolean isRefreshing,
            String supplierId,
            String tag,
            final OnLoadShopGoodsListener listener) {

        // 如果下拉刷新，则 还有更多数据 page = 1
        if (isRefreshing) {
            shopNewestGoodsPager = 1;
        }

        QueryShopNewestGoodsRequest request = new QueryShopNewestGoodsRequest();
        request.setUrl(Urls.SHOP_GOODS_LIST);
        request.setTag(tag);
        request.setSupplierId(supplierId);
        request.setToken(PreferenceUtils.getToken(UIUtils.getContext()));
        request.setPage(shopNewestGoodsPager);
        request.setPageSize(Common.PAGER_SIZE);

        mNetBiz.sendGetRequest(
                request,
                QueryShopGoodsResponse.class,
                new NetListener<QueryShopGoodsResponse>() {
                    @Override
                    public void onSuccess(QueryShopGoodsResponse response) {

                        // pager 增加
                        shopNewestGoodsPager++;
                        List<Goods> goodsLines = response.getData().getGoods();
                        if (isRefreshing) {
                            mShopAllGoodsGridList.clear();
                            mShopAllGoodsList.clear();
                        }
                        mShopAllGoodsList.addAll(goodsLines);

                        for (int i = 0; i < goodsLines.size(); i += 2) {
                            Goods goods1 = null;
                            Goods goods2 = null;
                            try {
                                goods1 = goodsLines.get(i);
                                goods2 = goodsLines.get(i + 1);
                            } catch (IndexOutOfBoundsException e) {
                            }
                            mShopAllGoodsGridList.add(new DoubleGoods(goods1, goods2));
                        }

                        if (listener != null) {
                            listener.onSuccess(
                                    mShopAllGoodsList,
                                    mShopAllGoodsGridList,
                                    goodsLines.size() == 0,
                                    response.hasMoreData());
                        }
                    }

                    @Override
                    public boolean onError(NetErrorInfo netErrorInfo) {
                        if (listener != null) {
                            listener.onFailed();
                        }
                        return true;
                    }

                    @Override
                    public boolean onFailed(BizErrorInfo bizErrorInfo) {
                        if (listener != null) {
                            listener.onFailed();
                        }
                        return true;
                    }

                    @Override
                    public void onFinal() {
                        if (listener != null) {
                            listener.onFinal();
                        }
                    }
                });
    }

    @Override
    public void loadShopPriceGoods(
            final boolean isRefreshing,
            boolean isSortUp,
            String supplierId,
            String tag,
            final OnLoadShopGoodsListener listener) {

        if (isRefreshing) {
            shopPriceGoodsPager = 1;
        }

        QueryShopPriceGoodsRequest request = new QueryShopPriceGoodsRequest();
        request.setUrl(Urls.SHOP_GOODS_LIST);
        request.setTag(tag);
        request.setSupplierId(supplierId);
        request.setPrice(isSortUp
                ? QueryShopPriceGoodsRequest.SORT_UP
                : QueryShopPriceGoodsRequest.SORT_DOWN);
        request.setToken(PreferenceUtils.getToken(UIUtils.getContext()));
        request.setPage(shopPriceGoodsPager);
        request.setPageSize(Common.PAGER_SIZE);

        mNetBiz.sendGetRequest(request, QueryShopGoodsResponse.class,
                new NetListener<QueryShopGoodsResponse>() {
                    @Override
                    public void onSuccess(QueryShopGoodsResponse response) {

                        shopPriceGoodsPager++;

                        List<Goods> goodsLines = response.getData().getGoods();
                        if (isRefreshing) {
                            mShopAllGoodsGridList.clear();
                            mShopAllGoodsList.clear();
                        }
                        mShopAllGoodsList.addAll(goodsLines);

                        for (int i = 0; i < goodsLines.size(); i += 2) {
                            Goods goods1 = null;
                            Goods goods2 = null;
                            try {
                                goods1 = goodsLines.get(i);
                                goods2 = goodsLines.get(i + 1);
                            } catch (IndexOutOfBoundsException e) {
                            }
                            mShopAllGoodsGridList.add(new DoubleGoods(goods1, goods2));
                        }

                        if (listener != null) {
                            listener.onSuccess(
                                    mShopAllGoodsList,
                                    mShopAllGoodsGridList,
                                    goodsLines.size() == 0,
                                    response.hasMoreData());
                        }
                    }

                    @Override
                    public boolean onError(NetErrorInfo netErrorInfo) {
                        return true;
                    }

                    @Override
                    public boolean onFailed(BizErrorInfo bizErrorInfo) {
                        return true;
                    }

                    @Override
                    public void onFinal() {
                        if (listener != null) {
                            listener.onFinal();
                        }
                    }
                });
    }

    @Override
    public void loadShopSalesGoods(
            final boolean isRefreshing,
            String supplierId,
            String tag,
            final OnLoadShopGoodsListener listener) {

        if (isRefreshing) {
            shopSalesGoodsPager = 1;
        }

        QueryShopSalesRequest request = new QueryShopSalesRequest();
        request.setUrl(Urls.SHOP_GOODS_LIST);
        request.setTag(tag);
        request.setSalesNum(QueryShopSalesRequest.SORT_DOWN);
        request.setSupplierId(supplierId);
        request.setToken(PreferenceUtils.getToken(UIUtils.getContext()));
        request.setPage(shopSalesGoodsPager);
        request.setPageSize(Common.PAGER_SIZE);

        mNetBiz.sendGetRequest(request, QueryShopGoodsResponse.class,
                new NetListener<QueryShopGoodsResponse>() {
                    @Override
                    public void onSuccess(QueryShopGoodsResponse response) {

                        shopSalesGoodsPager++;
                        List<Goods> goodsLines = response.getData().getGoods();
                        if (isRefreshing) {
                            mShopAllGoodsGridList.clear();
                            mShopAllGoodsList.clear();
                        }
                        mShopAllGoodsList.addAll(goodsLines);
                        for (int i = 0; i < goodsLines.size(); i += 2) {
                            Goods goods1 = null;
                            Goods goods2 = null;
                            try {
                                goods1 = goodsLines.get(i);
                                goods2 = goodsLines.get(i + 1);
                            } catch (IndexOutOfBoundsException e) {
                            }
                            mShopAllGoodsGridList.add(new DoubleGoods(goods1, goods2));
                        }

                        if (listener != null) {
                            listener.onSuccess(
                                    mShopAllGoodsList,
                                    mShopAllGoodsGridList,
                                    goodsLines.size() == 0,
                                    response.hasMoreData());
                        }
                    }

                    @Override
                    public boolean onError(NetErrorInfo netErrorInfo) {
                        return true;
                    }

                    @Override
                    public boolean onFailed(BizErrorInfo bizErrorInfo) {
                        return true;
                    }

                    @Override
                    public void onFinal() {
                        if (listener != null) {
                            listener.onFinal();
                        }
                    }
                });
    }

    @Override
    public void collectShop(
            String supplierId, boolean isCollect, String tag, final OnAccessListener listener) {

        CollectShopRequest request = new CollectShopRequest();
        request.setUrl(Urls.COLLECT_SHOP);
        request.setToken(PreferenceUtils.getToken(UIUtils.getContext()));
        request.setTag(tag);
        request.setSupplierId(supplierId);
        request.setType(isCollect
                ? CollectShopRequest.TYPE_COLLECT
                : CollectShopRequest.TYPE_CANCEL_COLLECT);

        mNetBiz.sendGetRequest(request, BaseResponse.class, new NetListener<BaseResponse>() {
            @Override
            public void onSuccess(BaseResponse response) {
                if (listener != null) {
                    listener.onSuccess();
                }
            }

            @Override
            public boolean onError(NetErrorInfo netErrorInfo) {
                return true;
            }

            @Override
            public boolean onFailed(BizErrorInfo bizErrorInfo) {
                return true;
            }

            @Override
            public void onFinal() {
            }
        });
    }

    @Override
    public void loadShopCategory(
            String supplierId, String tag, final OnLoadShopCategoryListener listener) {

        QueryShopCategoryRequest request = new QueryShopCategoryRequest();
        request.setToken(PreferenceUtils.getToken(UIUtils.getContext()));
        request.setUrl(Urls.SHOP_CATEGORY);
        request.setSupplierId(supplierId);
        request.setTag(tag);

        mNetBiz.sendGetRequest(request, QueryShopCategoryResponse.class,
                new NetListener<QueryShopCategoryResponse>() {
                    @Override
                    public void onSuccess(QueryShopCategoryResponse response) {
                        QueryShopCategoryResponse.Data data = response.getData();
                        List<QueryShopCategoryResponse.ShopCategory> shopCategoryList =
                                data.getCatList();
                        if (listener != null) {
                            listener.onSuccess(shopCategoryList);
                        }
                    }

                    @Override
                    public boolean onError(NetErrorInfo netErrorInfo) {
                        if (listener != null) {
                            listener.onFailed();
                        }
                        return false;
                    }

                    @Override
                    public boolean onFailed(BizErrorInfo bizErrorInfo) {
                        if (listener != null) {
                            listener.onFailed();
                        }
                        return true;
                    }

                    @Override
                    public void onFinal() {
                    }
                });
    }

    @Override
    public void loadCategoryNewGoodsList(
            final boolean isRefreshing,
            String supplierId,
            String categoryId,
            String tag,
            final OnLoadShopGoodsListener listener) {

        if (isRefreshing) {
            categoryNewGoodsPager = 1;
        }

        QueryShopCategoryNewGoodsRequest request = new QueryShopCategoryNewGoodsRequest();
        request.setUrl(Urls.SHOP_CATEGORY_GOODS);
        request.setCategoryId(categoryId);
        request.setSupplierId(supplierId);
        request.setTag(tag);
        request.setToken(PreferenceUtils.getToken(UIUtils.getContext()));
        request.setPage(categoryNewGoodsPager);
        request.setPageSize(Common.PAGER_SIZE);
        request.setAddTime("1");

        mNetBiz.sendGetRequest(request, QueryGoodsListResponse.class,
                new NetListener<QueryGoodsListResponse>() {
                    @Override
                    public void onSuccess(QueryGoodsListResponse response) {

                        categoryNewGoodsPager++;

                        List<Goods> goodsLines = response.getData().getGoods();

                        if (isRefreshing) {
                            mCategoryGoodsGridList.clear();
                            mShopAllGoodsList.clear();
                        }
                        mShopAllGoodsList.addAll(goodsLines);
                        for (int i = 0; i < goodsLines.size(); i += 2) {
                            Goods goods1 = null;
                            Goods goods2 = null;
                            try {
                                goods1 = goodsLines.get(i);
                                goods2 = goodsLines.get(i + 1);
                            } catch (IndexOutOfBoundsException e) {
                            }
                            mCategoryGoodsGridList.add(new DoubleGoods(goods1, goods2));
                        }

                        if (listener != null) {
                            listener.onSuccess(
                                    mShopAllGoodsList,
                                    mCategoryGoodsGridList,
                                    goodsLines.size() == 0,
                                    response.hasMoreData());
                        }
                    }

                    @Override
                    public boolean onError(NetErrorInfo netErrorInfo) {
                        if (listener != null) {
                            listener.onFailed();
                        }
                        return true;
                    }

                    @Override
                    public boolean onFailed(BizErrorInfo bizErrorInfo) {
                        if (listener != null) {
                            listener.onFailed();
                        }
                        return true;
                    }

                    @Override
                    public void onFinal() {
                        if (listener != null) {
                            listener.onFinal();
                        }
                    }
                });
    }

    @Override
    public void loadCategoryPriceGoodsList(
            final boolean isRefreshing,
            String supplierId,
            String categoryId,
            boolean isSortUp,
            String tag,
            final OnLoadShopGoodsListener listener) {

        if (isRefreshing) {
            categoryPriceGoodsPager = 1;
        }

        QueryShopCategoryPriceNewGoodsRequest request = new QueryShopCategoryPriceNewGoodsRequest();
        request.setUrl(Urls.SHOP_CATEGORY_GOODS);
        request.setCategoryId(categoryId);
        request.setSupplierId(supplierId);
        request.setTag(tag);
        request.setPrice(isSortUp
                ? QueryShopCategoryPriceNewGoodsRequest.SORT_UP
                : QueryShopCategoryPriceNewGoodsRequest.SORT_DOWN);
        request.setToken(PreferenceUtils.getToken(UIUtils.getContext()));
        request.setPage(categoryPriceGoodsPager);
        request.setPageSize(Common.PAGER_SIZE);

        mNetBiz.sendGetRequest(request, QueryGoodsListResponse.class,
                new NetListener<QueryGoodsListResponse>() {
                    @Override
                    public void onSuccess(QueryGoodsListResponse response) {

                        categoryPriceGoodsPager++;

                        List<Goods> goodsLines = response.getData().getGoods();

                        if (isRefreshing) {
                            mCategoryGoodsGridList.clear();
                            mShopAllGoodsList.clear();
                        }
                        mShopAllGoodsList.addAll(goodsLines);

                        for (int i = 0; i < goodsLines.size(); i += 2) {
                            Goods goods1 = null;
                            Goods goods2 = null;
                            try {
                                goods1 = goodsLines.get(i);
                                goods2 = goodsLines.get(i + 1);
                            } catch (IndexOutOfBoundsException e) {
                            }
                            mCategoryGoodsGridList.add(new DoubleGoods(goods1, goods2));
                        }

                        if (listener != null) {
                            listener.onSuccess(
                                    mShopAllGoodsList,
                                    mCategoryGoodsGridList,
                                    goodsLines.size() == 0,
                                    response.hasMoreData());
                        }
                    }

                    @Override
                    public boolean onError(NetErrorInfo netErrorInfo) {
                        if (listener != null) {
                            listener.onFailed();
                        }
                        return true;
                    }

                    @Override
                    public boolean onFailed(BizErrorInfo bizErrorInfo) {
                        if (listener != null) {
                            listener.onFailed();
                        }
                        return true;
                    }

                    @Override
                    public void onFinal() {
                        if (listener != null) {
                            listener.onFinal();
                        }
                    }
                });
    }

    @Override
    public void loadCategorySalesGoodsList(
            final boolean isRefreshing,
            String supplierId,
            String categoryId,
            String tag,
            final OnLoadShopGoodsListener listener) {

        if (isRefreshing) {
            categorySalesGoodsPager = 1;
        }

        QueryShopCategorySalesGoodsRequest request = new QueryShopCategorySalesGoodsRequest();
        request.setUrl(Urls.SHOP_CATEGORY_GOODS);
        request.setCategoryId(categoryId);
        request.setSupplierId(supplierId);
        request.setTag(tag);
        request.setSalesNum(QueryShopCategorySalesGoodsRequest.SORT_DOWN);
        request.setToken(PreferenceUtils.getToken(UIUtils.getContext()));
        request.setPage(categorySalesGoodsPager);
        request.setPageSize(Common.PAGER_SIZE);

        mNetBiz.sendGetRequest(request, QueryGoodsListResponse.class,
                new NetListener<QueryGoodsListResponse>() {
                    @Override
                    public void onSuccess(QueryGoodsListResponse response) {

                        categorySalesGoodsPager++;

                        List<Goods> goodsLines = response.getData().getGoods();

                        if (isRefreshing) {
                            mCategoryGoodsGridList.clear();
                            mShopAllGoodsList.clear();
                        }
                        mShopAllGoodsList.addAll(goodsLines);

                        for (int i = 0; i < goodsLines.size(); i += 2) {
                            Goods goods1 = null;
                            Goods goods2 = null;
                            try {
                                goods1 = goodsLines.get(i);
                                goods2 = goodsLines.get(i + 1);
                            } catch (IndexOutOfBoundsException e) {
                            }
                            mCategoryGoodsGridList.add(new DoubleGoods(goods1, goods2));
                        }

                        if (listener != null) {
                            listener.onSuccess(
                                    mShopAllGoodsList,
                                    mCategoryGoodsGridList,
                                    goodsLines.size() == 0,
                                    response.hasMoreData());
                        }
                    }

                    @Override
                    public boolean onError(NetErrorInfo netErrorInfo) {
                        if (listener != null) {
                            listener.onFailed();
                        }
                        return true;
                    }

                    @Override
                    public boolean onFailed(BizErrorInfo bizErrorInfo) {
                        if (listener != null) {
                            listener.onFailed();
                        }
                        return true;
                    }

                    @Override
                    public void onFinal() {
                        if (listener != null) {
                            listener.onFinal();
                        }
                    }
                });
    }
}
